package com.meihua.baoleyi.view;

import android.annotation.TargetApi;
import android.content.Context;
import android.os.Build;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.meihua.baoleyi.R;

/**
 * Created by jzm on 2017/11/2.
 * 支付方式
 */

public class PayWayView extends LinearLayout {
    private ImageView wxFlag;
    private ImageView balanceFlag;
    private ImageView zhifubaoFlag;
    private LinearLayout wx;
    private LinearLayout balance;
    private LinearLayout zhifubao;

    private static final int PAY_WX = 1;
    private static final int PAY_BALANCE = 2;
    private static final int PAY_ZHIFUBAO = 3;

    private int currentFrag = PAY_WX;

    public PayWayView(Context context) {
        super(context);
    }

    public PayWayView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
    public PayWayView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        View.inflate(context, R.layout.layout_pay_way, this);
        wxFlag = (ImageView)findViewById(R.id.wxFlag);
        balanceFlag = (ImageView)findViewById(R.id.balance_flag);
        zhifubaoFlag = (ImageView)findViewById(R.id.zhifubao_flag);
        wx = (LinearLayout) findViewById(R.id.wx);
        balance = (LinearLayout) findViewById(R.id.balance);
        zhifubao = (LinearLayout) findViewById(R.id.zhifubao);
        setCurrentFrag(PAY_WX);
        setListener();
    }
    /**
     * 点击事件
     */
    private void setListener() {
        wx.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                setCurrentFrag(PAY_WX);
                if (onPaycheckInterface != null) {
                    onPaycheckInterface.onPayCheck(currentFrag);
                }
            }
        });
        balance.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                setCurrentFrag(PAY_BALANCE);
                if (onPaycheckInterface != null) {
                    onPaycheckInterface.onPayCheck(currentFrag);
                }
            }
        });
        zhifubao.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                setCurrentFrag(PAY_ZHIFUBAO);
                if (onPaycheckInterface != null) {
                    onPaycheckInterface.onPayCheck(currentFrag);
                }
            }
        });
    }

    public void setCurrentFrag(int way) {
        currentFrag = way;
        balanceFlag.setVisibility(GONE);
        wxFlag.setVisibility(GONE);
        zhifubaoFlag.setVisibility(GONE);
        switch (way) {
            case PAY_WX:
                wxFlag.setVisibility(VISIBLE);
                break;
            case PAY_BALANCE:
                balanceFlag.setVisibility(VISIBLE);
                break;
            case PAY_ZHIFUBAO:
                zhifubaoFlag.setVisibility(VISIBLE);
                break;
        }
    }

    ///////////////////////////  定义支付选择的监听   ////////////////////////////////////

    public interface OnPaycheckInterface{
        void onPayCheck(int way);
    }

    private OnPaycheckInterface onPaycheckInterface;

    public void setOnPaycheckInterface(OnPaycheckInterface onPaycheckInterface) {
        this.onPaycheckInterface = onPaycheckInterface;
    }
}
